﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using KnowledgeKiosk.Objects;

namespace KnowledgeKiosk.Parsers
{
    /// <summary>
    /// Provides a way to deserialize XML data to Weather objects. 
    /// </summary>
    public class WeatherParser : IXmlToObjectParser<Weather>
    {
        #region IXmlToObjectParser<Weather> Members

        public static Dictionary<int, String> WEATHER_CONDITIONS = new Dictionary<int, string>
        {
            { 0, "Onbekend" },
            { 200, "Onweer met lichte regenval" },
            { 201, "Onweer met regenval" },
            { 202, "Onweer met hevige regenval" },
            { 210, "Onweer" },
            { 211, "Storm" },
            { 212, "Hevige storm" },
            { 221, "Hevige storm" },
            { 230, "Storm met lichte motregen" },
            { 231, "Storm met motregen" },
            { 232, "Storm met hevige motregen" },
            { 300, "Lichte motregen" },
            { 301, "Motregen" },
            { 302, "Hevige motregen" },
            { 310, "Afwisselend lichte regenval en motregen" },
            { 311, "Afwisselend regenval en motregen" },
            { 312, "Afwisselend hevige regenval en motregen" },
            { 321, "Stortbui" },
            { 500, "Lichte regenval" },
            { 501, "Regenachtig" },
            { 502, "Hevige regenval" },
            { 503, "Zeer hevige regenval" },
            { 504, "Extreme regenval" },
            { 511, "Bevroren regenval" },
            { 520, "Lichte stortbuien" },
            { 521, "Stortbuien" },
            { 522, "Hevige stortbuien" },
            { 600, "Lichte sneeuwval" },
            { 601, "Sneeuw" },
            { 602, "Hevige sneeuwval" },
            { 611, "IJzel" },
            { 621, "Sneeuwstorm" },
            { 701, "Mist" },
            { 711, "Rook" },
            { 721, "Wazige mist" },
            { 731, "Zanderig" },
            { 741, "Dikke mist" },
            { 800, "Zonnig" },
            { 801, "Licht bewolkt" },
            { 802, "Half bewolkt" },
            { 803, "Overwegend bewolkt" },
            { 804, "Zwaar bewolkt" },
            { 900, "Tornado" },
            { 901, "Tropische storm" },
            { 902, "Orkaan" },
            { 903, "Extreme kou" },
            { 904, "Extreme hitte" },
            { 905, "Zware windstoten" },
            { 906, "Hagel" },
        };        

        /// <summary>
        /// Deserializes XML to a single Weather object.
        /// </summary>
        /// <param name="xml">The XML data to deserialize.</param>
        /// <returns>The deserialized Weather object.</returns>
        public Weather Parse(XElement xml)
        {
            var weather = new Weather();
            XElement weatherEl = xml.Element("list").Element("item");
            weather.City = (string)weatherEl.Element("name");
            var weatherId = (int)weatherEl.Element("weather").Element("item").Element("id");
            if (WEATHER_CONDITIONS.ContainsKey(weatherId))
                weather.Condition = WEATHER_CONDITIONS[weatherId];
            else
                weather.Condition = WEATHER_CONDITIONS[0];

            weather.Temperature = (double)weatherEl.Element("main").Element("temp") - 273.15;
            weather.Humidity = (double)weatherEl.Element("main").Element("humidity") / 100;
            weather.WindSpeed = (double)weatherEl.Element("wind").Element("speed") * 3.6;
            weather.WindDirection = (double)weatherEl.Element("wind").Element("deg");
            weather.IconPath = String.Format(@"Images/Weather/{0}.png", weatherId);
            return weather;
        }

        #endregion
    }
}